﻿using System;
using System.Collections.Generic;
using Identity;

namespace Coder.Workflow
{
    public class ProcessInstance
    {
        protected ProcessInstance()
        {
        }

        protected ProcessInstance(WorkProcess workProcess, User user)
        {
            CreateTime = DateTimeOffset.Now;
            Status = ProcessInstanceStatus.Created;
            WorkProcess = workProcess ?? throw new ArgumentNullException(nameof(workProcess));
            User = user ?? throw new ArgumentNullException(nameof(user));
        }


        public int Id { get; set; }
        public WorkProcess WorkProcess { get; set; }

        public ProcessInstanceStatus Status { get; protected set; }

        public DateTimeOffset? FinishTime { get; set; }

        public DateTimeOffset CreateTime { get; set; }

        public User User { get; set; }

        public string Number { get; set; }

        /// <summary>
        ///     启动工作流实例
        /// </summary>
        /// <param name="nexInstanceActivity"></param>
        /// <returns></returns>
        public bool Start(out IEnumerable<WorkActivity> nexInstanceActivity)
        {
            Status = ProcessInstanceStatus.Processing;
            nexInstanceActivity = WorkProcess.Start().Create(this);

            return true;
        }

        /// <summary>
        ///     工作流完成，需要执行的动作
        /// </summary>
        public virtual void Complete()
        {
            Status = ProcessInstanceStatus.Completed;
            FinishTime = DateTimeOffset.Now;
        }
    }
}